package com.wetalk.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;

public class ImageUtil {
	
	/**
     * 截取圆形图片,并加白边
     * @param bitmap 源Bitmap
     * @return
     */
    public static Bitmap getCircleBitmap(Bitmap bitmap) {
        int x = bitmap.getWidth();
        int y = bitmap.getHeight();
        final int color = 0xffffffff;
        Paint paint = new Paint();
        // 根据原来图片大小画一个矩形
        Rect rect;
        int min;
        if(x>y){
        	rect = new Rect(x/2-y/2, 0,x/2+y/2 , y);
        	min = y;
        }else{
        	rect = new Rect(0, y/2-x/2, x, y/2+x/2);
        	min = x;
        }
        
        Bitmap output = Bitmap.createBitmap(min,
                min, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        paint.setAntiAlias(true);
        paint.setColor(color);
        // 画出一个圆
        canvas.drawCircle(min/2, min/2, min/2, paint);
        //canvas.translate(-25, -6);
        // 取两层绘制交集,显示上层
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        
        // 将图片画上去
        canvas.drawBitmap(bitmap, rect, new Rect(0, 0, min, min), paint);
        paint = new Paint();
        paint.setColor(0xffffffff);
        int board_rate = 15;
        int d = min+min/board_rate;
        Bitmap output2 = Bitmap.createBitmap(d,
                d, Config.ARGB_8888);
        canvas = new Canvas(output2);
        canvas.drawCircle(d/2, d/2, d/2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
        // 将circle draw 画上去
        rect = new Rect(0, 0, min,min);
        canvas.drawBitmap(output, rect, new Rect(min/board_rate/2, min/board_rate/2, d-min/board_rate/2,d-min/board_rate/2), paint);

        
        // 返回Bitmap对象
        return output2;
    }

}
